using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Game.Utils
{
	public class ParseUtil
	{
		private static byte b;

		private static byte[] byte16 = new byte[2];

		private static byte[] byte32 = new byte[4];

		private static byte[] byte64 = new byte[8];

		private static byte[] bytes = new byte[2048];

		public static void WriteByte(MemoryStream ms, byte b)
		{
			ms.WriteByte(b);
		}

		public static byte ReadByte(MemoryStream ms)
		{
			b = (byte)ms.ReadByte();
			if (b >> 7 > 0)
			{
				return (byte)(b - 256);
			}
			return b;
		}

		public static void WriteBool(MemoryStream ms, bool i)
		{
			ms.WriteByte((byte)(i ? 1 : 0));
		}

		public static bool ReadBool(MemoryStream ms)
		{
			return ms.ReadByte() == 1;
		}

		public static void WriteUInt16(MemoryStream ms, int val)
		{
			byte16 = BitConverter.GetBytes((ushort)val);
			if (BitConverter.IsLittleEndian)
			{
				Byte2Reverse(byte16);
			}
			ms.Write(byte16, 0, 2);
		}

		public static void WriteUInt16(MemoryStream ms, uint val)
		{
			byte16 = BitConverter.GetBytes((ushort)val);
			if (BitConverter.IsLittleEndian)
			{
				Byte2Reverse(byte16);
			}
			ms.Write(byte16, 0, 2);
		}

		public static uint ReadUInt16(MemoryStream ms)
		{
			ms.Read(byte16, 0, byte16.Length);
			if (BitConverter.IsLittleEndian)
			{
				Byte2Reverse(byte16);
			}
			return BitConverter.ToUInt16(byte16, 0);
		}

		public static void WriteInt16(MemoryStream ms, int val)
		{
			byte16 = BitConverter.GetBytes((short)val);
			if (BitConverter.IsLittleEndian)
			{
				Byte2Reverse(byte16);
			}
			ms.Write(byte16, 0, 2);
		}

		public static int ReadInt16(MemoryStream ms)
		{
			ms.Read(byte16, 0, byte16.Length);
			if (BitConverter.IsLittleEndian)
			{
				Byte2Reverse(byte16);
			}
			return BitConverter.ToInt16(byte16, 0);
		}

		public static void WriteInt32(MemoryStream ms, int val)
		{
			byte32 = BitConverter.GetBytes(val);
			if (BitConverter.IsLittleEndian)
			{
				Byte4Reverse(byte32);
			}
			ms.Write(byte32, 0, 4);
		}

		public static int ReadInt32(MemoryStream ms)
		{
			ms.Read(byte32, 0, byte32.Length);
			if (BitConverter.IsLittleEndian)
			{
				Byte4Reverse(byte32);
			}
			return BitConverter.ToInt32(byte32, 0);
		}

		public static void WriteLong(MemoryStream ms, long i)
		{
			byte64 = BitConverter.GetBytes(i);
			if (BitConverter.IsLittleEndian)
			{
				Byte8Reverse(byte64);
			}
			ms.Write(byte64, 0, 8);
		}

		public static long ReadLong(MemoryStream ms)
		{
			ms.Read(byte64, 0, byte64.Length);
			if (BitConverter.IsLittleEndian)
			{
				Byte8Reverse(byte64);
			}
			return BitConverter.ToInt64(byte64, 0);
		}

		public static void WriteFloat32(MemoryStream ms, float i)
		{
			byte32 = BitConverter.GetBytes(i);
			if (BitConverter.IsLittleEndian)
			{
				Byte4Reverse(byte32);
			}
			ms.Write(byte32, 0, 4);
		}

		public static float ReadFloat32(MemoryStream ms)
		{
			ms.Read(byte32, 0, 4);
			if (BitConverter.IsLittleEndian)
			{
				Byte4Reverse(byte32);
			}
			return BitConverter.ToSingle(byte32, 0);
		}

		public static void WriteString(MemoryStream ms, string s)
		{
			if (string.IsNullOrEmpty(s))
			{
				WriteUInt16(ms, 0);
				return;
			}
			byte[] array = Encoding.UTF8.GetBytes(s);
			WriteUInt16(ms, (uint)array.Length);
			ms.Write(array, 0, array.Length);
		}

		public static string ReadString(MemoryStream ms)
		{
			int num = ReadInt16(ms);
			if (num == 0)
			{
				return string.Empty;
			}
			ms.Read(bytes, 0, num);
			return Encoding.UTF8.GetString(bytes, 0, num);
		}

		public static void WriteArray(MemoryStream ms, byte[] array)
		{
			if (array == null)
			{
				WriteUInt16(ms, 0);
				return;
			}
			WriteUInt16(ms, array.Length);
			foreach (byte b in array)
			{
				WriteByte(ms, b);
			}
		}

		public static byte[] ReadByteArray(MemoryStream ms)
		{
			uint num = ReadUInt16(ms);
			if (num == 0)
			{
				return null;
			}
			byte[] array = new byte[num];
			for (int i = 0; i < num; i++)
			{
				array[i] = ReadByte(ms);
			}
			return array;
		}

		public static void WriteArray(MemoryStream ms, int[] array)
		{
			if (array == null)
			{
				WriteUInt16(ms, 0);
				return;
			}
			WriteUInt16(ms, array.Length);
			foreach (int val in array)
			{
				WriteInt32(ms, val);
			}
		}

		public static int[] ReadInt32Array(MemoryStream ms)
		{
			uint num = ReadUInt16(ms);
			if (num == 0)
			{
				return null;
			}
			int[] array = new int[num];
			for (int i = 0; i < num; i++)
			{
				array[i] = ReadInt32(ms);
			}
			return array;
		}

		public static void WriteArray(MemoryStream ms, float[] array)
		{
			if (array == null)
			{
				WriteUInt16(ms, 0);
				return;
			}
			WriteUInt16(ms, array.Length);
			foreach (float i2 in array)
			{
				WriteFloat32(ms, i2);
			}
		}

		public static float[] ReadFloat32Array(MemoryStream ms)
		{
			uint num = ReadUInt16(ms);
			if (num == 0)
			{
				return null;
			}
			float[] array = new float[num];
			for (int i = 0; i < num; i++)
			{
				array[i] = ReadFloat32(ms);
			}
			return array;
		}

		public static void WriteArray(MemoryStream ms, string[] array)
		{
			if (array == null)
			{
				WriteUInt16(ms, 0);
				return;
			}
			WriteUInt16(ms, array.Length);
			foreach (string s in array)
			{
				WriteString(ms, s);
			}
		}

		public static string[] ReadStringArray(MemoryStream ms)
		{
			uint num = ReadUInt16(ms);
			if (num == 0)
			{
				return null;
			}
			string[] array = new string[num];
			for (int i = 0; i < num; i++)
			{
				array[i] = ReadString(ms);
			}
			return array;
		}

		public static void WriteList(MemoryStream ms, List<int> list)
		{
			if (list == null)
			{
				WriteUInt16(ms, 0);
				return;
			}
			WriteUInt16(ms, list.Count);
			foreach (int item in list)
			{
				WriteInt32(ms, item);
			}
		}

		public static List<int> ReadInt32List(MemoryStream ms)
		{
			uint num = ReadUInt16(ms);
			List<int> list = new List<int>();
			for (int i = 0; i < num; i++)
			{
				list.Add(ReadInt32(ms));
			}
			return list;
		}

		public static void WriteList(MemoryStream ms, List<string> list)
		{
			if (list == null)
			{
				WriteUInt16(ms, 0);
				return;
			}
			WriteUInt16(ms, list.Count);
			foreach (string item in list)
			{
				WriteString(ms, item);
			}
		}

		public static List<string> ReadStringList(MemoryStream ms)
		{
			uint num = ReadUInt16(ms);
			List<string> list = new List<string>();
			for (int i = 0; i < num; i++)
			{
				list.Add(ReadString(ms));
			}
			return list;
		}

		public static void Byte2Reverse(byte[] bytes)
		{
			b = bytes[0];
			bytes[0] = bytes[1];
			bytes[1] = b;
		}

		public static void Byte4Reverse(byte[] bytes)
		{
			b = bytes[0];
			bytes[0] = bytes[3];
			bytes[3] = b;
			b = bytes[1];
			bytes[1] = bytes[2];
			bytes[2] = b;
		}

		public static void Byte8Reverse(byte[] bytes)
		{
			b = bytes[0];
			bytes[0] = bytes[7];
			bytes[7] = b;
			b = bytes[1];
			bytes[1] = bytes[6];
			bytes[6] = b;
			b = bytes[2];
			bytes[2] = bytes[5];
			bytes[5] = b;
			b = bytes[3];
			bytes[3] = bytes[4];
			bytes[4] = b;
		}
	}
}
